package com.fdb.efp.console.api.rest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.irdstudio.basic.framework.core.constant.PubConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.irdstudio.efp.console.common.ConsoleConstant;
import com.irdstudio.efp.console.service.facade.PrdInfoService;
import com.irdstudio.efp.console.service.facade.PrdRuleRelService;
import com.irdstudio.efp.console.service.vo.PrdRuleRelVO;
import com.irdstudio.efp.e4a.service.facade.SRoleDataRuleService;
import com.irdstudio.basic.framework.core.constant.ApplicationConstance;
import com.irdstudio.basic.framework.core.constant.DataRuleType;
import com.irdstudio.basic.framework.core.constant.ResponseData;
import com.irdstudio.basic.framework.web.controller.AbstractController;

@RestController
@RequestMapping("/api")
public class PrdRuleRelController extends AbstractController  {
	
	@Autowired
	@Qualifier("prdRuleRelService")
	private PrdRuleRelService prdRuleRelService;
	@Autowired
	@Qualifier("prdInfoService")
	private PrdInfoService prdInfoService;
	@Autowired
	@Qualifier("sRoleDataRuleService")
	private SRoleDataRuleService sRoleDataRuleService;
	
	/**
	 * 列表数据查询
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping(value="/prd/rule/rels", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<PrdRuleRelVO>> queryPrdRuleRelAll(
			@RequestBody PrdRuleRelVO vo) {		
		// 根据用户角色获取用户权限方案
		List<PrdRuleRelVO> outputVo = null;
		Map<String,String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
		vo.setPrdCodebyBaseInfo(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PRD_CODE));
		DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
		setUserInfoToVO(vo);
		// 根据权限方案调用不同的方案进行数据查询
		switch (dataRuleType) {
		case Owner:
			outputVo = prdRuleRelService.queryAllOwner(vo);
			break;
		case CurrOrg:
			outputVo = prdRuleRelService.queryAllCurrOrg(vo);
			break;
		case CurrDownOrg:
			outputVo = prdRuleRelService.queryAllCurrDownOrg(vo);
			break;
		default:
			outputVo = prdRuleRelService.queryAllOwner(vo);
			break;
		}
		return getResponseData(outputVo);
	}
	
	/**
	 * 列表数据查询
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping(value="/prd/rule/relsByPrdId", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<PrdRuleRelVO>> queryAllByPrdId(
			@RequestBody PrdRuleRelVO vo) {
		//查询是否有数据权限
		Map<String, String> key = new HashMap<>();
		key.put("prdId", vo.getPrdId() );

		boolean checkRLT = this.checkOptionalAuthority(key, ConsoleConstant.URL_DATA_PERMISS_PRD_INFO, this.prdInfoService);
		if(!checkRLT)
			return ResponseData.create(null, ApplicationConstance.NO_AUTHORITY, ApplicationConstance.NO_AUTHORITY_MESSAGE, false);
		List<PrdRuleRelVO> outputVo = prdRuleRelService.queryAllByPrdId(vo);
		return getResponseData(outputVo);
	}
	
	/**
	 * 根据主键查询详情
	 * @return
	 */
	@RequestMapping(value="/prd/rule/rel/{prdId}/{ruleCollId}", method=RequestMethod.GET)
	public @ResponseBody ResponseData<PrdRuleRelVO> queryByPk(@PathVariable("prdId") String prdId,@PathVariable("ruleCollId") String ruleCollId) {		
		PrdRuleRelVO inVo = new PrdRuleRelVO();
				inVo.setPrdId(prdId);
				inVo.setRuleCollId(ruleCollId);
		PrdRuleRelVO outputVo = prdRuleRelService.queryByPk(inVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键删除信息
	 * @param prdRuleRel
	 * @return
	 */
	@RequestMapping(value="/prd/rule/rel", method=RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> deleteByPk(@RequestBody PrdRuleRelVO inPrdRuleRelVo) {		
		int outputVo = prdRuleRelService.deleteByPk(inPrdRuleRelVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键更新信息
	 * @param inPrdRuleRelVo
	 * @return
	 */
	@RequestMapping(value="/prd/rule/rel", method=RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateByPk(@RequestBody PrdRuleRelVO inPrdRuleRelVo) {		
		int outputVo = prdRuleRelService.updateByPk(inPrdRuleRelVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 新增数据
	 * @param inPrdRuleRelVo
	 * @return
	 */
	@RequestMapping(value="/prd/rule/rel", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertPrdRuleRel(@RequestBody PrdRuleRelVO inPrdRuleRelVo) {
		int outputVo = prdRuleRelService.insertPrdRuleRel(inPrdRuleRelVo);
		return getResponseData(outputVo);
		
	}
}
